package cn.iocoder.yudao.module.worker.service.goods;

import cn.iocoder.yudao.module.worker.controller.app.goods.vo.AppWorkerGoodsPageReqVO;
import cn.iocoder.yudao.module.worker.controller.app.goods.vo.AppWorkerGoodsSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.worker.controller.admin.goods.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.goods.WorkerGoodsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.worker.dal.mysql.goods.WorkerGoodsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.worker.enums.ErrorCodeConstants.*;

/**
 * 商品管理 Service 实现类
 *
 * @author Jerry
 */
@Service
@Validated
public class WorkerGoodsServiceImpl implements WorkerGoodsService {

    @Resource
    private WorkerGoodsMapper goodsMapper;

    @Override
    public Long createGoods(WorkerGoodsSaveReqVO createReqVO) {
        // 插入
        WorkerGoodsDO goods = BeanUtils.toBean(createReqVO, WorkerGoodsDO.class);
        goodsMapper.insert(goods);
        // 返回
        return goods.getId();
    }

    @Override
    public void updateGoods(WorkerGoodsSaveReqVO updateReqVO) {
        // 校验存在
        validateGoodsExists(updateReqVO.getId());
        // 更新
        WorkerGoodsDO updateObj = BeanUtils.toBean(updateReqVO, WorkerGoodsDO.class);
        goodsMapper.updateById(updateObj);
    }

    @Override
    public void deleteGoods(Long id) {
        // 校验存在
        validateGoodsExists(id);
        // 删除
        goodsMapper.deleteById(id);
    }

    private void validateGoodsExists(Long id) {
        if (goodsMapper.selectById(id) == null) {
            throw exception(GOODS_NOT_EXISTS);
        }
    }

    @Override
    public WorkerGoodsDO getGoods(Long id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public PageResult<WorkerGoodsDO> getGoodsPage(WorkerGoodsPageReqVO pageReqVO) {
        return goodsMapper.selectPage(pageReqVO);
    }

    @Override
    public Long createGoods(AppWorkerGoodsSaveReqVO createReqVO) {
        // 插入
        WorkerGoodsDO goods = BeanUtils.toBean(createReqVO, WorkerGoodsDO.class);
        goodsMapper.insert(goods);
        // 返回
        return goods.getId();
    }

    @Override
    public void updateGoods(AppWorkerGoodsSaveReqVO updateReqVO) {
        // 校验存在
        validateGoodsExists(updateReqVO.getId());
        // 更新
        WorkerGoodsDO updateObj = BeanUtils.toBean(updateReqVO, WorkerGoodsDO.class);
        goodsMapper.updateById(updateObj);
    }

    @Override
    public PageResult<WorkerGoodsDO> getGoodsPage(AppWorkerGoodsPageReqVO pageReqVO) {
        return goodsMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateGoodsStatus(Long id, Boolean status) {
        WorkerGoodsDO goodsDO = goodsMapper.selectById(id);
        goodsDO.setStatus(status);
        goodsMapper.updateById(goodsDO);
    }


}